package com.aizhixin.lab.company.service;

import com.aizhixin.lab.company.entity.ReleaseNotice;
import com.aizhixin.lab.company.entity.User;
import com.aizhixin.lab.company.repository.ReleaseNoticeRepository;
import com.aizhixin.lab.task.entity.ReleaseTask;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.domain.PageDomain;
import com.aizhixin.lab.company.domain.NoticeDomain;
import com.aizhixin.lab.company.entity.Notice;
import com.aizhixin.lab.company.repository.NoticeRepository;

@Slf4j
@Service
@Transactional
public class NoticeService {
	@Autowired
	private NoticeRepository noticeRepository;
	@Autowired
	private ReleaseNoticeRepository releaseNoticeRepository;
	@Autowired
	private UserService userService;

	public Map<String, Object> save(NoticeDomain domain) {
		Map<String, Object> result = new HashMap<>();
		Notice notice = null;
		if (null != domain.getId() && domain.getId() > 0) {
			notice = noticeRepository.findOne(domain.getId());
			if (null != notice) {
				notice.setName(domain.getName());
				notice.setContent(domain.getContent());
				notice.setLastModifiedDate(new Date());
			}else {
				result.put(ApiReturnConstants.ERROR, "公告信息不存在");
				result.put(ApiReturnConstants.SUCCESS, false);
				return result;
			}
		}else {
			notice = new Notice();
			BeanUtils.copyProperties(domain, notice);
			notice.setDeleteFlag(2);
		}
	
		noticeRepository.save(notice);
		result.put(ApiReturnConstants.SUCCESS, true);

		return result;
	}
	
	public Map<String, Object> changeStatus(Long id,Integer flag) {
		Map<String, Object> result = new HashMap<>();
		if (null != id && id > 0) {
			Notice notice = noticeRepository.findOne(id);
			if (null != notice) {
				notice.setDeleteFlag(flag);
				notice.setLastModifiedDate(new Date());
				noticeRepository.save(notice);
				if (flag==1){
					releaseNoticeRepository.deleteByParentId(id);
				}else {
					List<User> users = userService.findUsers();
					List<ReleaseNotice> releaseNotices=new ArrayList<>();
					for (User user:users){
						ReleaseNotice releaseNotice = new ReleaseNotice();
						releaseNotice.setParentId(notice.getId());
						releaseNotice.setName(notice.getName());
						releaseNotice.setContent(notice.getContent());
						releaseNotice.setCreatedDate(new Date());
						releaseNotice.setUserId(user.getId());
						releaseNotice.setStatus(DataValidity.VALID.getState());
						releaseNotices.add(releaseNotice);
					}
					releaseNoticeRepository.save(releaseNotices);
				}
				result.put(ApiReturnConstants.SUCCESS, true);
			}else {
				result.put(ApiReturnConstants.ERROR, "公告信息不存在");
				result.put(ApiReturnConstants.SUCCESS, false);
				return result;
			}
		}
		return result;
	}
	
	
	public Map<String, Object> getOne(Long id) {
		Map<String, Object> result = new HashMap<>();
		if (null != id && id > 0) {
			ReleaseNotice releaseNotice = releaseNoticeRepository.findOne(id);
			if (null != releaseNotice) {
				releaseNotice.setStatus(DataValidity.INVALID.getState());
				releaseNoticeRepository.save(releaseNotice);
				result.put(ApiReturnConstants.DATA, releaseNotice);
				result.put(ApiReturnConstants.SUCCESS, true);
			}else {
				result.put(ApiReturnConstants.ERROR, "公告信息不存在");
				result.put(ApiReturnConstants.SUCCESS, false);
				return result;
			}
		}
		return result;
	}
	
	public Map<String, Object> getPageForAdmin(Pageable pageable, String name) {
		Map<String, Object> result = new HashMap<>();
		Page<Notice> page = null;
		if(StringUtils.isEmpty(name)) {
			page = noticeRepository.findAllByDeleteFlagNot(pageable,DataValidity.INVALID.getState());
		}else {
			page = noticeRepository.findAllByNameLikeAndDeleteFlagNot(pageable,"%"+name+"%",DataValidity.INVALID.getState());
		}
		PageDomain p = new PageDomain();
		p.setPageNumber(pageable.getPageNumber());
		p.setPageSize(pageable.getPageSize());
		p.setTotalPages(page.getTotalPages());
		p.setTotalElements(page.getTotalElements());
		result.put(ApiReturnConstants.PAGE, p);
		result.put(ApiReturnConstants.DATA, page.getContent());
		result.put(ApiReturnConstants.SUCCESS, true);
		return result;
	}
	
	public Map<String, Object> getPage(Pageable pageable, String name,Long userId) {
		Map<String, Object> result = new HashMap<>();
		Page<ReleaseNotice> page = null;
		if(StringUtils.isEmpty(name)) {
			page = releaseNoticeRepository.findAllByDeleteFlagAndUserId(pageable,DataValidity.VALID.getState(),userId);
		}else {
			page = releaseNoticeRepository.findAllByNameLikeAndDeleteFlagAndUserId(pageable,"%"+name+"%",DataValidity.VALID.getState(),userId);
		}
		PageDomain p = new PageDomain();
		p.setPageNumber(pageable.getPageNumber());
		p.setPageSize(pageable.getPageSize());
		p.setTotalPages(page.getTotalPages());
		p.setTotalElements(page.getTotalElements());
		result.put(ApiReturnConstants.PAGE, p);
		result.put(ApiReturnConstants.DATA, page.getContent());
		result.put(ApiReturnConstants.SUCCESS, true);
		return result;
	}
}
